﻿using Platformer_Example.Content.Images.Player;
using Platformer_Example.Controller.Input;
using System;
using Platformer_Example.Controller.Terrain;
using Transformable_Engine_v2.Engine.Animation.FrameAnimation;
using Transformable_Engine_v2.Engine.Time;
using Transformable_Engine_v2.Engine.Transformable_Math.Geometry;

namespace Platformer_Example.Controller.Character.Player
{
    public partial class Player
    {
        protected override int JumpLimit
        {
            get { return 1; }
        }

        public override float JumpHeight
        {
            get { return -0.75f; }
        }

        private Timer _jumpTimer;

        private bool _isJumpButtonReleased;

        protected override void CreateJumpStateObjects()
        {
            AddTimers(_jumpTimer = new Timer(275, false, null));
        }

        protected override void InitializeJumpState()
        {
            _jumpTimer.Start();

            _isJumpButtonReleased = false;

            if (IsCarryingObject)
            {
                Character.Sprite = SpritesheetPlayer.GetSprite(SpritesheetPlayer.JumpState.JumpCarry);
                Character.OriginAbsolute = new TE_Vector2(8, 32);
                CarriedObject.Position = new TE_Vector2(0, -21);
            }
            else
            {
                Character.Sprite = SpritesheetPlayer.GetSprite(SpritesheetPlayer.JumpState.Jump);
                Character.OriginAbsolute = new TE_Vector2(8, 32);
            }
            
        }

        protected override void UpdateJumpState(TimeSpan gameTime)
        {
            ChangeDirectionFromInput();
            ChangeWalkSpeedFromInput();

            ApplyGravity(gameTime);

            ApplyMovementX(gameTime);
            var collisionResult = ApplyMovementY(gameTime, true);
            if (collisionResult == BaseTerrain.TerrainType.Floor)
                ChangeState(State.Idle);
            else if (collisionResult == BaseTerrain.TerrainType.Ceiling)
            {
                ResetGravityY();
                _isJumpButtonReleased = true;
            }

            else if(GravityVector.Y + PushbackVector.Y > 0)
                ChangeState(State.Fall);

            else if(_jumpTimer.IsStarted && !_isJumpButtonReleased && InputListener.KeyState[InputState.Key.ButtonSouth])
                Jump(JumpHeight);

            if (InputListener.KeyState[InputState.Key.ButtonWest])
            {
                if (IsCarryingObject)
                {
                    ChangeState(BaseInputCharacter.State.AirThrow);
                }
            }

            ApplyWarps();
        }

        protected override void EndJumpState()
        {
        }

        protected override void OnKeyPressJump(InputState.Key key)
        {
        }

        protected override void OnKeyReleaseJump(InputState.Key key)
        {
            if (key == InputState.Key.ButtonSouth)
            {
                _isJumpButtonReleased = true;
            }
        }
    }
}